define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置

            var window_innerheight = window.innerHeight;
            var table_height = window_innerheight-20;
            Table.api.init({
                extend: {
                    index_url: 'general/data/index',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    dragsort_url: '',
                    table: 'data',
                },
                pageSize: Config.pagesize,
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('the_name'),operate: false},
                        {
                            field: 'file_size', title: __('Filesize'), operate: false, formatter: function (value, row, index) {
                                if(value == 0){
                                    return 0;
                                }
                                var size = parseFloat(value);
                                var i = Math.floor(Math.log(size) / Math.log(1024));
                                return (size / Math.pow(1024, i)).toFixed(i < 2 ? 0 : 2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
                            }
                        },
                        {field: 'create_time', title: __('Create time'),operate: false},

                        {
                            field: 'url', title: __('Download Link'),operate: false,formatter: function (value, row, index) {
                               return "<a href='"+value+"' target='_blank'>"+value+"</a>";
                            }
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);


            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        add: function () {
            Controller.api.bindevent();
        },
        test: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                setRiqi:function (value, row, index){
                    if(value  == ''){
                        return '-';
                    }else{
                        return value.substr(0,10);
                    }
                },
                setType:function (value, row, index){
                    if(value  == 1){
                        return 'TLS';
                    }else{
                        return 'SSL';
                    }
                },
                setStatus:function (value, row, index) {
                    if(row.status == -1){
                        var html = '<span class="text-error"><i class="fa fa-circle"></i>'+__('Disabled')+'</span>';
                    }else if(row.status == 1){
                        var html = '<span class="text-success"><i class="fa fa-circle"></i>'+__('Enable')+'</span>';
                    }else{
                        var html = '<span class="text-danger"><i class="fa fa-circle"></i>'+__('Unknown')+'</span>';//6
                    }
                    return html;
                }
            }
        }
    };
    return Controller;
});
